package saint.bicyletracker;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;
import saint.bicyletracker.UI.SpeedDetail;
import saint.manager.*;

public class MainActivity extends Activity {

	GpsManager gpsManager;
	GPSManagerListener gpsManagerListener;
	SpeedDetail speedDetail;
	
	private int locUpdate = 0;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Implement class.
        gpsManager = new GpsManager(getApplicationContext());
        gpsManagerListener = new GPSManagerCallback();
        
        // Check GPS
        gpsManager.checkGPSEnable();
        
        // Set GPSManager callback
        gpsManager.setGPSManagerListener(gpsManagerListener);
        
        // Start GPS
        gpsManager.startGPS();
        
        // Implement UI
        speedDetail = (SpeedDetail)findViewById(R.id.speedDetail1);
        speedDetail.setUnit(UnitConvert.SPEED_UNIT_STRING[gpsManager.getSpeedUnit()]);
        
        // Get location info
        GpsInfo gpsInfo = gpsManager.isHasGpsInfo();
        if(gpsInfo != null) {
        	/*TextView acc = (TextView)findViewById(R.id.acc_val_lb);
			TextView spd = (TextView)findViewById(R.id.spd_val);
			TextView lat = (TextView)findViewById(R.id.lat_val_lb);
			TextView longi = (TextView)findViewById(R.id.long_val_lb);
			
			acc.setText(Double.toString(gpsInfo.getAccuracy()) + " m");
			spd.setText(String.format("%.2f", gpsInfo.getSpeed()));
			lat.setText(Double.toString(gpsInfo.getLatitude()));
			longi.setText(Double.toString(gpsInfo.getLongitude()));*/
        }
    }
    
    @Override
    public void onDestroy() {
    	super.onDestroy();
    	gpsManager.stopGPS();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
    private class GPSManagerCallback implements GPSManagerListener {

		public void onGPSUpdate(int event, GpsInfo info) {
			//TextView sta_update = (TextView)findViewById(R.id.sta_update);
			
			switch (event) {
				case GpsManager.GPS_LOCATION_UPDATE :
					/*TextView acc = (TextView)findViewById(R.id.acc_val_lb);
					TextView spd = (TextView)findViewById(R.id.spd_val);
					TextView lat = (TextView)findViewById(R.id.lat_val_lb);
					TextView longi = (TextView)findViewById(R.id.long_val_lb);
					TextView distance = (TextView)findViewById(R.id.dis_val_lb);
					
					
					double _acc = info.getAccuracy();
					double _spd = info.getSpeed();
					double _lat = info.getLatitude();
					double _longi = info.getLongitude();
					double _dis = info.getDistance();
					
					acc.setText(Double.toString(_acc) + " m");
					spd.setText(String.format("%.2f", _spd));
					lat.setText(Double.toString(_lat));
					longi.setText(Double.toString(_longi));
					distance.setText(String.format("%.3f " + UnitConvert.DISTANT_UINT_STRING[UnitConvert.KILOMETER], _dis));
					sta_update.setText("Location " + Integer.toString(locUpdate));
					
					if(++locUpdate > 100) { locUpdate = 0; }*/
					speedDetail.setSpeedText(String.format("%.2f", info.getSpeed()));
					
					break;
				case GpsManager.GPS_STATUS_UPDATE :
					/*TextView signal = (TextView)findViewById(R.id.gps_signal_lb);
					String signal_string;
					double signal_val;
					signal_val = gpsManager.getSNR();
					signal_string = String.format("%.2f", signal_val);
					signal.setText(signal_string);
					sta_update.setText("Signal");*/
					break;
			}
		}
    	
    }
}
